package com.csdj.t309springboot.controller;
import com.csdj.t309springboot.common.CacheConstant;
import com.csdj.t309springboot.common.WebResult;
import com.csdj.t309springboot.common.WebResultMsgCode;
import com.csdj.t309springboot.entity.BlindBox;
import com.csdj.t309springboot.entity.JigsawPuzzle;
import com.csdj.t309springboot.entity.JigsawPuzzlePieces;
import com.csdj.t309springboot.entity.PiecesOfRecord;
import com.csdj.t309springboot.mapper.BlindBoxMapper;
import com.csdj.t309springboot.service.JigsawPuzzlePiecesService;
import com.csdj.t309springboot.service.JigsawPuzzleService;
import com.csdj.t309springboot.service.PiecesOfRecordService;
import com.csdj.t309springboot.service.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 拼图表 前端控制器
 * </p>
 *
 * @author 谭湛宇
 * @since 2022-01-14
 */
@RestController
@RequestMapping("/jigsaw/puzzle")
public class JigsawPuzzleController extends BaseController{
    private Logger log = LoggerFactory.getLogger("");
    @Resource
    private JigsawPuzzleService jigsawPuzzleService;
    @Resource
    private PiecesOfRecordService piecesOfRecordService;
    @Resource
    private JigsawPuzzlePiecesService jigsawPuzzlePiecesService;
    @Resource
    private BlindBoxMapper blindBoxMapper;
    @Resource
    private RedisService redisService;

    /**
     * 领奖; 当所有的图块收集完成后才可以领奖
     * @param request
     * @return
     */
    @GetMapping("/accept/the/prize")
    public WebResult acceptThePrize(int jigsawPuzzleId,HttpServletRequest request){//
        int blindBoxId=0;
        int jigsawPuzzleQuantityCompleted=0;
        int count=0;
        int userId = getUid(request);
        log.info("jigsawPuzzleId="+jigsawPuzzleId);
        List<JigsawPuzzlePieces> list = jigsawPuzzlePiecesService
                .getJigsawPuzzlePiecesListByUserIdAndJigsawPuzzleIdAndBlindBoxId(userId, jigsawPuzzleId, blindBoxId);
        for (JigsawPuzzlePieces jigsawPuzzlePieces:list){

            log.info("jigsawPuzzlePieces.getPiecesPosition()="+jigsawPuzzlePieces.getPiecesPosition());

            for (int i = 0; i < 9; i++) {
                if( jigsawPuzzlePieces.getPiecesPosition()== (i+1) ){
                    if(jigsawPuzzlePieces.getNumberOfPuzzlePieces()>0){
                        ++count;
                    }
                }
            }
        }
        log.info("已完成数量 jigsawPuzzleQuantityCompleted="+jigsawPuzzleQuantityCompleted);
        if(count<9){
            return new WebResult(WebResultMsgCode.SUCCESS,"积分数量不足").putData("list",list);
        }else{
            return new WebResult(WebResultMsgCode.SUCCESS,true).putData("list",list);
        }
    }



    /**
     *  获取拼图信息
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/getJigsawPuzzleInfo")
    public WebResult getJigsawPuzzleInfo(int id, HttpServletRequest request){
        int jigsawPuzzleId = getJigsawPuzzleId(request);
        log.info("getJigsawPuzzleInfo() jigsawPuzzleId="+jigsawPuzzleId);
        log.info("getJigsawPuzzleInfo() id="+id);
        JigsawPuzzle jigsawPuzzle = jigsawPuzzleService.getJigsawPuzzleInfo(id);
        if(null!=jigsawPuzzle){
            return new WebResult(WebResultMsgCode.SUCCESS).putData("entity",jigsawPuzzle);
        }
        return new WebResult(WebResultMsgCode.FAIL);
    }

    /**
     * 根据用户编号和拼图编号和盲盒编号获取拼图碎片信息列表
     * @param request HttpServletRequest对象
     * @param jigsawPuzzleId 拼图编号
     * @param blindBoxId 盲盒编号
     * @return WebResult 返回固定的json数据格式
     */
    @GetMapping("/getJigsawPuzzlePiecesListByUserIdAndJigsawPuzzleIdAndBlindBoxId")
    public WebResult getPiecesOfRecordListByUserIdAndJigsawPuzzleIdAndBlindBoxId(
            HttpServletRequest request,int jigsawPuzzleId,int blindBoxId){
        int userId = getUid(request);
        List<JigsawPuzzlePieces> list = jigsawPuzzlePiecesService.getJigsawPuzzlePiecesListByUserIdAndJigsawPuzzleIdAndBlindBoxId(userId, jigsawPuzzleId, blindBoxId);
        return new WebResult(WebResultMsgCode.SUCCESS).putData("list",list);
    }

    /**
     *  获取所有拼图列表
     * @return
     */
    @GetMapping("/getAllJigsawPuzzle")
    public WebResult getAllJigsawPuzzle(){
        List<JigsawPuzzle> list = jigsawPuzzleService.getAllJigsawPuzzle();
        if(null!=list){
            return new WebResult(WebResultMsgCode.SUCCESS).putData("list",list);
        }
        return new WebResult(WebResultMsgCode.FAIL);
    }
}
